package com.example.shardingjdbctest.algorithm;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import org.springframework.stereotype.Component;

import java.util.Collection;
@Slf4j
@Component
public class MyTablePreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> {
    public MyTablePreciseShardingAlgorithm() {
    }

    @Override
    public String doSharding(Collection<String> tablesNames, PreciseShardingValue<Long> shardingValue) {
        log.info("=======分表开始=========");
        for(String tableName:tablesNames){
           String value =  shardingValue.getValue() % tablesNames.size() + 1 + "";
           if(tableName.endsWith(value)){
               log.info("当前表:"+tableName);
               log.info("=======分表结束=========");
               return tableName;
           }
        }

        throw new IllegalArgumentException();
    }
}
